import serial
import time

def send_data(rect_position,laser_center):
    try:
        # 配置串口
        ser = serial.Serial('/dev/ttyAMA0',9600)
        if not ser.is_open:
            ser.open()

        # 要发送的数据
        # 将数据转换为字符串格式
        send_str = f"{rect_position[0][0]} {rect_position[0][1]} {rect_position[1][0]} {rect_position[1][1]} " \
                   f"{rect_position[2][0]} {rect_position[2][1]} {rect_position[3][0]} {rect_position[3][1]} " \
                   f"{laser_center[0]} {laser_center[1]} \n"

        ser.write(send_str.encode())  # 发送字符串流
        print("已发送数据到STM32")
        print(f"发送的数据: {send_str}")


        time.sleep(1)

    except Exception as e:
        print(f"发生错误：{e}")
    finally:
        if ser.is_open:
            ser.close()
        print("串口已关闭")




